package com.evan.webadmin.biz.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evan.common.core.exception.MyRuntimeException;
import com.evan.webadmin.biz.dao.EvluateSurveyDetailMapper;
import com.evan.webadmin.biz.dao.EvluateSurveyMapper;
import com.evan.webadmin.biz.dto.EvluateSurveyDto;
import com.evan.webadmin.biz.dto.EvluateSurveyFilter;
import com.evan.webadmin.biz.model.EvluateSurvey;
import com.evan.webadmin.biz.model.EvluateSurveyDetail;
import com.evan.webadmin.biz.service.IEvluateRecordService;
import com.evan.webadmin.biz.service.IEvluateSurveyDetailService;
import com.evan.webadmin.biz.service.IEvluateSurveyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评价问卷表 服务实现类
 * </p>
 *
 * @author Pavel
 * @since 2025-03-04
 */
@Service
public class EvluateSurveyServiceImpl extends ServiceImpl<EvluateSurveyMapper, EvluateSurvey> implements IEvluateSurveyService {

    @Autowired
    private EvluateSurveyMapper surveyMapper;
    @Autowired
    private EvluateSurveyDetailMapper surveyDetailMapper;
    @Autowired
    private IEvluateSurveyDetailService detailService;
    @Autowired
    private IEvluateRecordService recordService;

    @Override
    public List<EvluateSurvey> findByPage(EvluateSurveyFilter surveyFilter) {
        QueryWrapper<EvluateSurvey> queryWrapper = new QueryWrapper<>();
        if (surveyFilter != null) {
            queryWrapper.likeRight("survey_name", surveyFilter.getSurveyName());
        }
        queryWrapper.eq("delete_flag", 0L);
        queryWrapper.orderByDesc("update_time");
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EvluateSurveyDto surveyDto) {
        EvluateSurvey survey;
        if(surveyDto.getId() != null) {
            survey = surveyMapper.selectById(surveyDto.getId());
        }else {
            survey = new EvluateSurvey();
        }
        BeanUtils.copyProperties(surveyDto, survey);
        survey.setSurveyCount(surveyDto.getSubjects().size());
        if(survey.getId() != null){
            survey.setUpdateTime(new Date());
            surveyMapper.update(survey, new QueryWrapper<EvluateSurvey>().eq("id", survey.getId()));
            surveyDetailMapper.deleteBySurveyId(survey.getId());
        }else{
            survey.setCreateTime(new Date());
            surveyMapper.insert(survey);
        }
        survey.setSurveyCount(surveyDto.getSubjects().size());
        List<EvluateSurveyDetail> details = new ArrayList<>();
        surveyDto.getSubjects().forEach(question -> {
            EvluateSurveyDetail detail = new EvluateSurveyDetail();
            detail.setSurveyId(survey.getId());
            detail.setSubjectId(question.getSubjectId());
            detail.setSubjectContent(JSONUtil.toJsonStr(question));
            detail.setCreateTime(new Date());
            details.add(detail);
        });
        detailService.saveBatch(details);
    }

    @Override
    public void publish(Long id) {
        EvluateSurvey survey = getById(id);
        if(survey != null){
            surveyMapper.resetPublish();
            survey.setPublished(true);
            surveyMapper.update(survey, new QueryWrapper<EvluateSurvey>().eq("id", id));
        }
    }

    @Override
    public EvluateSurvey findPublishedPaper() {
        QueryWrapper<EvluateSurvey> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("published", true);
        return surveyMapper.selectOne(queryWrapper);
    }

    @Override
    public void logicDelete(Long surveyId) {
        EvluateSurvey survey = surveyMapper.selectById(surveyId);
        if(survey == null){
            throw  new MyRuntimeException("不存在的问卷");
        }

        long count = recordService.existCount(surveyId);
        if(count > 0){
            throw new MyRuntimeException("存在评价记录，不能删除");
        }

        survey.setDeleteFlag(System.currentTimeMillis());
        survey.setUpdateTime(new Date());
        surveyMapper.update(survey, new QueryWrapper<EvluateSurvey>().eq("id", surveyId));
    }
}
